package com.ns.school.service.edu.impl;

import com.ns.school.po.edu.BookStockDB;
import com.ns.school.po.edu.LayuiResult;
import com.ns.school.mapper.edu.BookStockMapper;
import com.ns.school.service.edu.BookStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BookStockImpl implements BookStockService {
    @Autowired
    private BookStockMapper bookStockMapper;


    @Override
    public List<BookStockDB> findBookStock(BookStockDB bookStockDB) {
        return bookStockMapper.findBookStock(bookStockDB);
    }


    @Override
    public BookStockDB selectBookStock(Integer id) {
        return bookStockMapper.selectBookStock(id);
    }



    //入库操作
    public void addBookStockMapper(BookStockDB bookStockDB,LayuiResult result) {
        bookStockDB.setDeleteFlag(0);
        bookStockDB.setBookStatus(1);
        Integer addBookStock =bookStockMapper.addBookStock(bookStockDB);
        if (addBookStock > 0) {
            result.setCode(0);
            result.setMsg("入库成功");
        }
    }

    //图书审核
    @Transactional
    public void setBookStockMapper(LayuiResult result,BookStockDB params){
        int count=bookStockMapper.checkRecord(params);
        if (count > 0) {
            result.setCode(500);
            result.setMsg("该审核记录已处理，请勿重复审核。");
        } else {
            // 执行审核入库操作
            int audited =bookStockMapper.auditTrue(params.getStockId());
            if (audited == 1) {
                BookStockDB p=bookStockMapper.findBook(params);
                params.setStockCount(p.getStockCount());
                params.setBookId(p.getBookId());
                bookStockMapper.auditBook(params);
                result.setCode(0);
                result.setMsg("审核成功！");
            } else {
                result.setCode(500);
                result.setMsg("审核失败");
            }
        }
    }


    //审核拒绝
    @Transactional
    public void auditFailMapper(LayuiResult result,BookStockDB bookStockDB) {
        int count =bookStockMapper.checkRecord(bookStockDB);
        if (count > 0) {
            result.setCode(500);
            result.setMsg("该审核记录已处理，请勿重复审核。");
        } else {
            int audited =bookStockMapper.auditFail(bookStockDB.getStockId());
            if (audited == 1) {
                result.setCode(0);
                result.setMsg("审核拒绝！");
            } else {
                result.setCode(500);
                result.setMsg("审核失败");
            }
        }
    }

    //入库编辑
    @Transactional
    public void updateBookStockMapper(LayuiResult result,BookStockDB bookStockDB) {

        int a =bookStockMapper.updateBookStock(bookStockDB);
        if (a > 0) {
            result.setCode(0);
            result.setMsg("入库编辑成功");
        }
    }


}
